package com.oocl.formV1.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;


public class DBUtil {
//	private static BasicDataSource ds;
//	private static Properties pro;
//	private static InputStream in;
//	
//	static{
//		try {
//			ds = new BasicDataSource();
//			in = new BufferedInputStream(new FileInputStream("db.properties"));
//			pro = new Properties();
//			pro.load(in);
//			ds.setDriverClassName(pro.getProperty("driverClass"));
//			ds.setUsername(pro.getProperty("userName"));
//			ds.setPassword(pro.getProperty("pwd"));
//			ds.setUrl(pro.getProperty("url"));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	private static String driverClass = "oracle.jdbc.OracleDriver";
	private static String userName = "mike";
	private static String pwd = "123456";
	private static String url = "jdbc:oracle:thin:@10.222.29.165:1521:xe";
	private static BasicDataSource ds;
	
	static{
		ds = new BasicDataSource();
		ds.setDriverClassName(driverClass);
		ds.setUsername(userName);
		ds.setPassword(pwd);
		ds.setUrl(url);
	}
	
	/**
	 * 
	 * @return
	 */
	public static Connection createConnectionWithDataSource(){
		Connection con = null;
		try {
			con = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 
	 * @param con
	 * @param pst
	 * @param rs
	 */
	public static void close(Connection con, PreparedStatement pst, ResultSet rs){
		try {
			if(rs!=null)
				rs.close();
			if(pst!=null)
				pst.close();
			if(con!=null)
				con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
